package com.yanggu.jt808.util;

import com.yanggu.jt808.consts.Jt808Constant;
import com.yanggu.jt808.enums.CharsetType;
import com.yanggu.jt808.model.resp.RegisterResp;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * 响应-编码转换
 *
 * @Description ResHelper
 * @Author guoyb
 * @Date 2023/11/7 15:18
 */
@Slf4j
@Component
@AllArgsConstructor
public class RespHelper {


    /**
     * 包装命令
     *
     * @param resMsgId      平台响应消息ID
     * @param phoneNum      终端手机号原始报文
     * @param msgBody       消息体
     * @param platStreamNum 平台流水号(应答流水号)
     * @return 命令
     */
    private static byte[] warp(int resMsgId, byte[] phoneNum, byte[] msgBody, int platStreamNum) {
        byte[] msgId = {(byte) resMsgId, 0x00};
        int bodyLen = msgBody.length;
        if (phoneNum.length == 10) {
            // 2019
            return ByteArrHelper.union(
                    msgId,
                    new byte[]{(byte) (((bodyLen >>> 8) & 0x03) | 0x40), (byte) (bodyLen & 0xff), 0x01},
                    phoneNum,
                    new byte[]{(byte) ((platStreamNum >>> 8) & 0xff), (byte) (platStreamNum & 0xff)},
                    msgBody);
        } else if (phoneNum.length == 6) {
            // 2011 2013
            return ByteArrHelper.union(
                    msgId,
                    new byte[]{(byte) ((bodyLen >>> 8) & 0x03), (byte) (bodyLen & 0xff)},
                    phoneNum,
                    new byte[]{(byte) ((platStreamNum >>> 8) & 0xff), (byte) (platStreamNum & 0xff)},
                    msgBody);
        } else {
            return null;
        }
    }


}
